package com.tn.utils;


/**
 * @Author: Suzuki
 * @CreateTime: 2023-04-11  09:49
 * @Description: TODO
 * @Version: 1.0
 **/
public class HexUtil {

    /**
     * 16进制字符串转字节数组
     * @param strValue
     * @return
     */
    public static byte[] Hex2Bytes(String hexString){

        byte[] arrB = hexString.getBytes();
        int iLen = arrB.length;
        byte[] arrOut = new byte[iLen / 2];
        String strTmp = null;
        for (int i = 0; i < iLen; i += 2)
        {
            strTmp = new String(arrB, i, 2);
            arrOut[(i / 2)] = ((byte)Integer.parseInt(strTmp, 16));
        }
        return arrOut;
    }

    /**
     * 将字符串转成ASCII
     * @param strValue
     * @return
     */
    public static String stringToAscii(String strValue) {
        StringBuffer sbu = new StringBuffer();
        char[] chars = strValue.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                sbu.append((int) chars[i]).append(" ");
            } else {
                sbu.append((int) chars[i]);
            }
        }
        String s = sbu.toString();
        StringBuilder b=new StringBuilder();
        String[] split = s.split(" ");
        for (int i = 0; i < split.length; i++) {
            int s1 = Integer.valueOf(split[i]);
            String s2 = Integer.toHexString(s1).toUpperCase();
            if(i != split.length-1){
                b.append("0"+s2).append(" ");
            }else {
                b.append("0"+s2);
            }


        }
        return b.toString();
    }

//    public static void main(String[] args) {
//        String replace = "00 10 01 04 00 0F 01 10 00 01 00 04 08 00 01 00 02 00 01 00 02".replace(" ", "");
//        byte[] bytes = Hex2Bytes(replace);
//        String s = new String(bytes);
//        System.out.println(s);
//    }
}
